//
//  MyAccountViewController.swift
//  cxd4iphone
//
//  Created by hexy on 12/3/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit

class MyAccountViewController: BaseTableViewController {

    init() {
        super.init(style: .Grouped)
        
        self.title = "我的账户"
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        makeFooterView()
    }
    
    override func makeTableView() {
        
        tableView.separatorStyle = .SingleLine
        tableView.rowHeight = xx_height(44)
        tableView.separatorColor = xx_colorWithHex(hexValue: 0xededed)
        
        self.tableListViewModel = MyAccountListViewModel()
        tableView.registerClass(BaseTableViewCell.self, forCellReuseIdentifier: baseCellReuseId)
        tableView.sectionFooterHeight = 0.1
        
    }
    
    override func cellSelected(indexPath: NSIndexPath) {
        
        xx_print(indexPath.section)
        xx_print(indexPath.row)
        if 0 == indexPath.section {
            
            if 0 == indexPath.row {
                
                xx_showMaskHUD(nil)
                YPRequestViewModel.shared.changeMobile().subscribeError({ (error) -> Void in
                    
                    xx_print(error)
                    }) { () -> Void in
                        xx_hideHUD()
                        let vc = WebViewController(title: "修改手机号", htmlStr: YPRequestViewModel.shared.html)
                        self.navigationController?.pushViewController(vc, animated: true)
                }

            }
            
            if 2 == indexPath.row {
                
                YPRequestViewModel.shared.bindBankCard().subscribeError({ (error) -> Void in
                    
                    xx_print(error)
                    }) { () -> Void in
                        
                        let vc = WebViewController(title: "绑卡", htmlStr: YPRequestViewModel.shared.html)
                        self.navigationController?.pushViewController(vc, animated: true)
                }
            }
        }
        
        
        if 1 == indexPath.section {
            
            if 1 == indexPath.row {
                xx_showMaskHUD(nil)
                YPRequestViewModel.shared.changeTradePwd().subscribeError({ (error) -> Void in
                    
                    xx_showError(error)
                    }) { () -> Void in
                        
                        xx_hideHUD()
                        let vc = WebViewController(title: "重置交易密码", htmlStr: YPRequestViewModel.shared.html)
                        self.navigationController?.pushViewController(vc, animated: true)
                }
            }
        }
    }


}

extension MyAccountViewController {
    
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return xx_height(10)
    }
    
    
    func makeFooterView() {
        
        let footer = UIView(frame: CGRectMake(0, 0, xx_screenWidth(), xx_height(80)))
        let exitBtn = UIButton(title: "退出登录", titleColor: UIColor.whiteColor(), titleSize: 18, bgColor: Define.blueColor)
        
        exitBtn.rac_signalForControlEvents(.TouchUpInside).subscribeNext { (_) -> Void in
            
            AccountViewModel.shared.exit()
            
            NSNotificationCenter.defaultCenter().postNotificationName(Define.chooseRootViewControllerNotification, object: MyAccountViewController.self)
        }
        
        footer.addSubview(exitBtn)
        exitBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(footer)
            make.left.equalTo(footer).offset(15)
            make.right.equalTo(footer).offset(-15)
            make.height.equalTo(xx_height(40))
        }
        
        tableView.tableFooterView = footer
    }
}